using System;
using System.Collections.Generic;
using System.Text;
using IGGCore;
using IGG;

namespace IGG
{
    public class ModeGameFormatterBase : IModeFormatter
    {
        #region IModeFormatter Members

        public virtual void FormatMode(Scoreboard board)
        {
        }

        protected void ShowBoxscore(Game game)
        {
            int innCount = game.Status.Inning;
            Output.Write(" " + ToolBox.GetFormattedStringPadRight(game.AwayTeam.Abbrv.ToUpper(), 5));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.AwayTeam.Wins + "-" + game.AwayTeam.Losses, 8));
            for (int i = 0; i < innCount; i++)
            {
                if (game.Linescore.Innings[i].Away > -1)
                {
                    Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Away.ToString(), 2));
                }
                else
                {
                    Output.Write(ToolBox.GetFormattedStringPadRight("0", 2));
                }
            }
            Output.Write("  ");
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Runs.ToString(), 3));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Hits.ToString(), 3));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Errors.ToString(), 3));
            if (game.Status.InningTop == true && game.Status.Indicator.IsInProgress)
            {
                Output.Write("    " + game.Status.Strikes + " strikes, " + game.Status.Balls + " balls, "
                    + game.Status.Outs + " outs");
            }
            Output.WriteLine();


            Output.Write(" " + ToolBox.GetFormattedStringPadRight(game.HomeTeam.Abbrv.ToUpper(), 5));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.HomeTeam.Wins + "-" + game.HomeTeam.Losses, 8));
            for (int i = 0; i < innCount; i++)
            {
                if (game.Linescore.Innings[i].Home > -1)
                {
                    Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Home.ToString(), 2));
                }
                else
                {
                    Output.Write(ToolBox.GetFormattedStringPadRight(" ", 2));
                }
            }

            Output.Write("  ");
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Runs.ToString(), 3));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Hits.ToString(), 3));
            Output.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Errors.ToString(), 3));
            if (game.Status.InningTop == false && game.Status.Indicator.IsInProgress)
            {
                Output.Write("    " + game.Status.Strikes + " strikes, " + game.Status.Balls + " balls, "
                    + game.Status.Outs + " outs");
            }
            Output.WriteLine();
        }

        /// <summary>
        /// Show the loaded bases
        /// </summary>
        /// <param name="game"></param>
        protected void ShowLoadedBases(Game game)
        {
            if (game.Status.RunnersOnBase[Constants.FIRST_BASE] != null)
            {
                Output.WriteLine(" " + "B1: " + game.Status.RunnersOnBase[Constants.FIRST_BASE].PartialName);
            }
            else
            {
                Output.WriteLine(" " + "B1: ");
            }
            if (game.Status.RunnersOnBase[Constants.SECOND_BASE] != null)
            {
                Output.WriteLine(" " + "B2: " + game.Status.RunnersOnBase[Constants.SECOND_BASE].PartialName);
            }
            else
            {
                Output.WriteLine(" " + "B2: ");
            }
            if (game.Status.RunnersOnBase[Constants.THIRD_BASE] != null)
            {
                Output.WriteLine(" " + "B3: " + game.Status.RunnersOnBase[Constants.THIRD_BASE].PartialName);
            }
            else
            {
                Output.WriteLine(" " + "B3: ");
            }
        }

        /// <summary>
        /// Show the home runs
        /// </summary>
        /// <param name="game"></param>
        protected void ShowHomeRuns(Game game)
        {
            Output.Write(" HR: ");
            int count = 1;
            foreach (HomeRun hr in game.Status.HomeRuns)
            {
                Output.Write(hr.Player.PartialName + " (" + hr.StdHR + ")");
                if (count < game.Status.HomeRuns.Count)
                {
                    Output.Write(", ");
                }
                count++;
            }
            Output.WriteLine();
        }

        /// <summary>
        /// Show the game preview
        /// </summary>
        /// <param name="game">The game to preview</param>
        protected void ShowGamePreview(Game game)
        {
            Output.WriteLine(" " + game.AwayTeam.Name
                + " (" + game.AwayTeam.Wins + "-" + game.AwayTeam.Losses + ") vs "
                + game.HomeTeam.Name
                + " (" + game.HomeTeam.Wins + "-" + game.HomeTeam.Losses + ")");

            if (ToolBox.IsPreseason())
            {
                Output.WriteLine();
                Output.WriteLine(" This is a preseason game. "
                    + ToolBox.NumDayBeforeSeasonStart().ToString()
                    + " days before the season starts.");
            }
            else
            {
                //Console.WriteLine();
                if (game.AwayTeam.GamesBack == "-")
                {
                    Output.WriteLine(" " + game.AwayTeam.City + " is in first place.");
                }
                if (game.HomeTeam.GamesBack == "-")
                {
                    Output.WriteLine(" " + game.HomeTeam.City + " is in first place.");
                }
                //Console.WriteLine("");
            }

            //Console.WriteLine();
            Output.WriteLine(" " + game.Stadium.Name + ", "
                        + ToolBox.GetVenue(game.Stadium.VenueCode) + ", starting time at " + game.StartTime + " " + game.AmPm);

            if (game.HomeProbable != null)
            {
                Output.WriteLine();
                Output.WriteLine(" Pitching for " + game.HomeTeam.City + " is "
                    + game.HomeProbable.Name + " (" + game.HomeProbable.Wins
                    + "-" + game.HomeProbable.Losses + ", " + game.HomeProbable.ERA + ")");
                if (game.HomeProbable.Report != null && game.HomeProbable.Report != string.Empty)
                {
                    Output.FormatLongText(game.HomeProbable.Report);
                }

                Output.WriteLine();
                Output.WriteLine(" Pitching for " + game.AwayTeam.City + " is "
                    + game.AwayProbable.Name + " (" + game.AwayProbable.Wins
                    + "-" + game.AwayProbable.Losses + ", " + game.AwayProbable.ERA + ")");
                if (game.AwayProbable.Report != null && game.AwayProbable.Report != string.Empty)
                {
                    Output.FormatLongText(game.AwayProbable.Report);
                }

            }
        }

        /// <summary>
        /// Format the play by play to fit on one line
        /// </summary>
        /// <param name="pbp">play by play</param>
        /// <returns></returns>
        protected string FormatPBP(string pbp)
        {
            pbp = pbp.Replace("  ", " ");
            if (pbp.Trim().EndsWith("."))
            {
                pbp = pbp.Trim().Remove(pbp.Trim().Length - 1);
            }
            pbp = pbp.Replace("shortstop", "SS");
            pbp = pbp.Replace("Shortstop", "SS");
            pbp = pbp.Replace("first baseman", "1B");
            pbp = pbp.Replace("First baseman", "1B");
            pbp = pbp.Replace("second baseman", "2B");
            pbp = pbp.Replace("Second baseman", "2B");
            pbp = pbp.Replace("third baseman", "3B");
            pbp = pbp.Replace("Third baseman", "3B");
            pbp = pbp.Replace("left fielder", "LF");
            pbp = pbp.Replace("Left fielder", "LF");
            pbp = pbp.Replace("center fielder", "CF");
            pbp = pbp.Replace("Center fielder", "CF");
            pbp = pbp.Replace("right fielder", "RF");
            pbp = pbp.Replace("Right fielder", "RF");
            pbp = pbp.Replace("Substitution", "sub");
            pbp = pbp.Replace("substitution", "sub");
            pbp = pbp.Replace("Defensive", "Def");
            pbp = pbp.Replace("defensive", "def");
            pbp = pbp.Replace("right field", "RF");
            pbp = pbp.Replace("left field", "LF");
            pbp = pbp.Replace("center field", "CF");
            pbp = pbp.Replace(" (1) ", " ");
            pbp = pbp.Replace(" (2) ", " ");
            pbp = pbp.Replace(" (3) ", " ");
            pbp = pbp.Replace(" (4) ", " ");
            pbp = pbp.Replace(" (5) ", " ");
            pbp = pbp.Replace(" (6) ", " ");
            int inx = pbp.IndexOf(", batting");
            if (inx > -1)
            {
                pbp = pbp.Substring(0, inx);
            }
            return pbp;
        }

        protected void ShowTeamsWins(Game game)
        {
            Output.Write(" The " + game.AwayTeam.Name + " are " + game.AwayTeam.Wins + "-" + game.AwayTeam.Losses + "");
            Output.Write(" and the " + game.HomeTeam.Name + " are " + game.HomeTeam.Wins + "-" + game.HomeTeam.Losses + ".");
            Output.WriteLine();
        }

        /// <summary>
        /// Show deck and hole
        /// </summary>
        /// <param name="game"></param>
        protected void ShowDeckHole(Game game, string battingTeam)
        {
            string deck = " " + "On Deck: "
                + battingTeam + " "
                + game.Status.OnDeck.FullName;
                //+ "; " + "In Hole: "
                //+ battingTeam + " "
                //+ game.Status.InHole.PartialName;
            Output.WriteLine(deck);
        }


        #endregion
    }
}
